package com.unsiv.helicopter.entity;

import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import com.unsiv.helicopter.Command;

import android.util.Log;
import android.view.MotionEvent;

public class Channel extends AnimatedSprite {

	int channelFlag;

	public Channel(float pX, float pY, float pTileWidth, float pTileHeight, TiledTextureRegion pTiledTextureRegion) {
		super(pX, pY, pTileWidth, pTileHeight, pTiledTextureRegion);
	}

	public Channel(float pX, float pY, TiledTextureRegion pTiledTextureRegion) {
		super(pX, pY, pTiledTextureRegion);
	}

	@Override
	public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
		switch (pSceneTouchEvent.getAction()) {
		case MotionEvent.ACTION_DOWN:
			final float width = this.getWidth();
			final float unitWidth = width / 4f;
			final int flag = (int) (pTouchAreaLocalX / unitWidth);
			if (flag >= 0 && flag < 4) {
				this.setCurrentTileIndex(0,flag);
				channelFlag = flag;
				Command.mChannel = flag;
				Log.d("Channel", String.valueOf(flag));
			}
			break;
		}
		return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
	}

	@Override
	public void setCurrentTileIndex(int pTileIndex) {
		return;
		//super.setCurrentTileIndex(pTileIndex);
	}
	
	@Override
	public void setCurrentTileIndex(int pTileColumn, int pTileRow) {
		if(pTileColumn!=0 || pTileRow<0 || pTileRow>4)
			channelFlag = pTileRow;
		super.setCurrentTileIndex(pTileColumn, pTileRow);
	}

	public int getChannelValue() {
		return channelFlag;
	}
}
